package com.igeek.collateral.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.collateral.entity.Role;
import com.igeek.collateral.entity.RoleMenu;
import com.igeek.collateral.entity.RolePermission;
import com.igeek.collateral.mapper.RoleMapper;
import com.igeek.collateral.mapper.RoleMenuMapper;
import com.igeek.collateral.mapper.RolePermissionMapper;
import com.igeek.collateral.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.collateral.vo.PageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-04
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Transactional(readOnly = true)
    @Override
    public Page<Role> findPage(PageQueryVO vo) {
        Integer currentPage = vo.getCurrentPage();
        Integer pageSize = vo.getPageSize();
        String queryString = vo.getQueryString();
        //封装Page对象
        Page<Role> page = new Page<>(currentPage , pageSize);
        //封装查询条件
        LambdaQueryWrapper<Role> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.hasLength(queryString) , Role::getName , queryString);
        //分页查询数据
        Page<Role> RolePage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
        return RolePage;
    }

    @Override
    public boolean addMenuToRole(Integer[] menuIds, Integer roleId) {
        boolean addMenuToRole1 = addMenuToRole1(menuIds, roleId);
        return addMenuToRole1;
    }


    @Override
    public List<Integer> findPermissionId(Integer roleId) {
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(new LambdaQueryWrapper<RolePermission>().eq(RolePermission::getRoleId, roleId));
        List<Integer> permissionIds = rolePermissions.stream().map(RolePermission::getPermissionId).collect(Collectors.toList());
        return permissionIds;
    }

    @Override
    public boolean del(Integer rid) {
        roleMenuMapper.delete(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, rid));
        int i=this.baseMapper.deleteById(rid);
        return i>0?true:false;
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer roleId) {
        List<RoleMenu> list = roleMenuMapper.selectList(new LambdaQueryWrapper<RoleMenu>().eq(RoleMenu::getRoleId, roleId));
        List<Integer> menuIds=list.stream().map(RoleMenu::getRoleId).collect(Collectors.toList());
        return menuIds;
    }




    private boolean addMenuToRole1(Integer[] menuIds, Integer roleId){
        Stream.of(menuIds).forEach(menuId->{
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(roleId);
            roleMenu.setMenuId(menuId);
            roleMenuMapper.insert(roleMenu);
        });
        return true;
    }
}
